
// 表单验证
function Testing() {
    this.phonenum = $("#phonenum");
    this.bindEvent();
}
Testing.prototype.bindEvent = function () {
    var _this = this;
    this.phonenum.blur(function () {
        this.tip = $("#name-tip");
        var user_value = this.value;
        if (user_value === "") {
            validate_result[0].pass = false;
            this.tip.css({
                display: "block"
            });
            this.tip.html("请输入手机号码");
            _this.phonenum.css({
                borderColor: "red"
            });
        } else {
            validate_result[0].pass = true;
            this.tip.css({
                display: "none"
            });
            _this.phonenum.css({
                borderColor: "#" + "ddd"
            });
        }
    })
}
new Testing();


// 密码验证
var psw_type = false;
var phonenum = $("#phonenum");
var psw_input = $("#password");
$(psw_input).blur(function () {

    var tip = $("#psw-tip");
    var psw_value = this.value;
    if (psw_value === "") {
        validate_result[1].pass = false;
        psw_type = false;
        tip.css({
            display: "block",
            color: "red"
        });
        tip.html("密码不能为空");
    } else {
        validate_result[1].pass = true;
        psw_type = true;
        tip.css({
            display: "none",
        });
    }
})

var btn = document.getElementById("btn");
// 创建一个存储表单验证状态的变量; 
var validate_result = [
    {
        ele: phonenum,
        pass: false
    },
    {
        ele: psw_input,
        pass: false
    }
]
btn.onclick = function () {
    // 遍历表单验证列表;
    for (var i = 0; i < validate_result.length; i++) {
        // 查看是不是有没有通过验证的元素，如果有就终止程序执行; 
        if (!validate_result[i].pass) {
            validate_result[i].ele.focus();
            return false;
            // 应该终止程序执行; 
        }
    }
}



// ajax 操作

// var phonenum = getCookie("phonenum");
// var password = getCookie("password");
// var phonenum_ipt = $("#phonenum");
// var password_ipt = $("#password");
// var btn = $("#btn");
// if(phonenum && password){
//     phonenum_ipt.val(phonenum);
//     password_ipt.val(password);
// } 
// btn.on("click" , function(){
//     var phonenum_value = phonenum_ipt.val();
//     var password_value = password_ipt.val();
//     if(psw_type){
//         $.ajax({
//             url : "http://localhost/server/sign.php",
//             dataType : "json",
//             data :({
//                 phonenum : phonenum_value,
//                 password : password_value,
//                 type : "login"
//             })
//         })
//         .then(function(data){
//             if(data.type === "success"){
//                 alert("登录成功，即将跳转到首页");
//                 location.href = "index.html";
//                 // 记住密码
//               cookie("phonenum" , phonenum_value , {
//                 expirs : 15
//               });
//               cookie("password" , password_value , {
//                 expirs : 15
//               });
//             //   cookie("state" , );
//             }else{
//                 alert("帐号或密码错误，请重新输入");
//             }
//         })
//     }   
//   })


var phonenum = getCookie("phonenum");
var password = getCookie("password");
var phonenum_ipt = document.querySelector("#phonenum");
var password_ipt = document.querySelector("#password");
var btn = document.querySelector("#btn");
if (phonenum && password) {
    phonenum_ipt.value = phonenum;
    password_ipt.value = password;
}
btn.addEventListener("click", function () {
    var phonenum_value = phonenum_ipt.value;
    var password_value = password_ipt.value;

    if (psw_type) {
        var url = "http://localhost/server/sign.php";
        url += "?phonenum=" + phonenum_value + "&password=" + password_value + "&type=login";
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url);
        xhr.send(null);
        xhr.onload = function () {
            var data = JSON.parse(xhr.responseText);
            if (data.type === "success") {
                // 记住密码
                cookie("phonenum", phonenum_value, {
                    expirs: 15
                });
                cookie("password", password_value, {
                    expirs: 15
                });

                cookie("state", xhr.responseText);
                alert("登录成功，即将跳转到首页");
                location.href = "index.html";
            } else {
                alert("帐号或密码错误，请重新输入");
            }

        }
    }
})